package com.huanzhidadi.es.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * ElasticSearch 客户端配置
 *
 * @author LiuHuan
 * @date 2023年06月19日17:42
 */
@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.host-and-port}")
    private String hostAndPort; // 127.0.0.1:9200

    @Value("${elasticsearch.connectTimeout}")
    private Long connectTimeout;

    @Value("${elasticsearch.socketTimeout}")
    private Long socketTimeout;

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(hostAndPort)
                .withConnectTimeout(connectTimeout)
                .withSocketTimeout(socketTimeout)
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}
